package com.seveneight.baijiesteward.ui.user.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.adapter.CreditAdapter;
import com.seveneight.baijiesteward.base.BaseActivity;
import com.seveneight.baijiesteward.bean.AnyBean;
import com.seveneight.baijiesteward.bean.CreditBean;
import com.seveneight.baijiesteward.http.AppConfig;
import com.seveneight.baijiesteward.http.MyCallBack;
import com.seveneight.baijiesteward.ui.main.activity.WebActivity;
import com.seveneight.baijiesteward.ui.user.contract.CreditCardListContract;
import com.seveneight.baijiesteward.ui.user.model.BindCreditCardUrlModel;
import com.seveneight.baijiesteward.ui.user.presenter.CreditCardListPresenter;
import com.seveneight.baijiesteward.utils.LogUtil;
import com.seveneight.baijiesteward.utils.RecyclerviewInitUtil;
import com.seveneight.baijiesteward.utils.WindowUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by user1 on 2018/3/28.
 */

public class CreditCardListActivity extends BaseActivity implements CreditCardListContract.CreditCardListView, View.OnClickListener {

    CreditCardListContract.CreditCardListPresenter creditCardListPresenter;
    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_right)
    TextView tv_right;
    @BindView(R.id.recyclerview)
    LRecyclerView recyclerview;
    private CreditAdapter creditAdapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    BindCreditCardUrlModel bindCard=new BindCreditCardUrlModel();

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_list);
    }

    @Override
    protected void initView() {
        tv_title.setText("选择信用卡");
        RecyclerviewInitUtil.initRefreshLinearReccycler(activity, recyclerview, R.dimen.divider_1dp);
        recyclerview.setPullRefreshEnabled(false);
        creditAdapter = new CreditAdapter(activity);
        lRecyclerViewAdapter = new LRecyclerViewAdapter(creditAdapter);
        recyclerview.setAdapter(lRecyclerViewAdapter);
    }

    @Override
    protected void initData() {
        creditCardListPresenter = new CreditCardListPresenter(this);
        creditCardListPresenter.getCreditCardList(activity,jsonFormat.commonParam());
    }


    @Override
    protected void setListener() {
        iv_back.setOnClickListener(this);
        tv_right.setOnClickListener(this);

        recyclerview.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {

            }
        });
        lRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (creditAdapter.getDataList().get(position).getUnionPayCert()){
                    CreditBean creditBean = creditAdapter.getDataList().get(position);
                    Intent intent = new Intent();
                    intent.putExtra("bean", creditBean);
                    setResult(RESULT_OK, intent);
                    finish();
                }else {
                    shwoDialog(creditAdapter.getDataList().get(position).getCreditCardID());
                }
            }
        });

    }
    public void shwoDialog(String id){
                new MaterialDialog.Builder(this)
                        .title("该卡未认证")
                        .titleColorRes(R.color.colorAccent)
                .content("必须进行认证后才能使用")
                .contentColorRes(R.color.graytext)
                .backgroundColorRes(R.color.white)
                .negativeColorRes(R.color.mainblue)
                .negativeText(R.string.negative)
                .positiveColorRes(R.color.mainblue)
                .positiveText(R.string.positive)
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        dialog.dismiss();
                        Map<String,String> params=new HashMap<>();
                        params.put("CreditCardID",id);
                        bindCard.getBankList(getContext(), jsonFormat.AddCard(params), new MyCallBack<AnyBean>() {
                            @Override
                            public void onSuccess(AnyBean anyBean) {
                                Intent intent=new Intent(getContext(), WebActivity.class);
                                Bundle bundle5 = new Bundle();
                                bundle5.putString("url", anyBean.getBindInfo());
                                intent.putExtras(bundle5);
                                startActivityIfNeeded(intent,11);
//                                WindowUtil.getInstance().startActivity(getContext(), WebActivity.class, bundle5);
                            }

                            @Override
                            public void onFailed() {

                            }
                        });
                    }
                })
                .build().show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
            creditCardListPresenter.getCreditCardList(activity,jsonFormat.commonParam());
        }
    }

    @Override
    public void reBackCreditCardList(List<CreditBean> data) {
        LogUtil.d("data" + data.size() + "");
        recyclerview.refreshComplete(data.size());
        creditAdapter.addAll(data);
        lRecyclerViewAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_right:
                Bundle bundle = new Bundle();
                bundle.putString("url", AppConfig.FAST_REPAY + setting.getUserID() + "&Signature=123");
                WindowUtil.getInstance().startActivity(activity, WebActivity.class, bundle);
                break;

        }
    }
}
